<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryParams"
        size="small"
        label-width="88px"
        class="formBox"
      >
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="订单号" prop="orderNumber">
              <el-input
                v-model="queryParams.orderNumber"
                placeholder="请输入订单号"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入姓名"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="身份证号" prop="idCardNumber">
              <el-input
                v-model="queryParams.idCardNumber"
                placeholder="请输入身份证号"
                maxlength="18"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select
                v-model="queryParams.paymentMethod"
                placeholder="请选择支付方式"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.payment_method"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="补缴状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="请选择补缴状态"
                clearable
                style="width: 100%"
              >
                <el-option label="已补缴" value="1" />
                <el-option label="未补缴" value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="学校" prop="school">
              <el-input
                v-model="queryParams.school"
                placeholder="请输入学校"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="年级" prop="grade">
              <el-input
                v-model="queryParams.grade"
                placeholder="请输入年级"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="班级" prop="className">
              <el-input
                v-model="queryParams.className"
                placeholder="请输入班级"
                maxlength="50"
                show-word-limit
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="支付时间" prop="time">
              <el-date-picker
                v-model="queryParams.time"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 按钮 -->
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['order:abnormal:export']"
            @click="dealMoreErrorOrderFlowFun"
          >
            批量处理异常订单
          </el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <!-- 列表 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="recordList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="订单号" align="center" prop="orderNumber" width="300" />
        <el-table-column label="订单金额" align="center" prop="orderAmount">
          <template slot-scope="scope">
            {{ scope.row.orderAmount ? scope.row.orderAmount : "0" }}元
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column
          label="身份证号"
          align="center"
          prop="idCardNumber"
          width="180"
        />
        <!-- <el-table-column label="设备编号" align="center" prop="snCode" width="180" /> -->
        <el-table-column label="设备名称" align="center" prop="deviceName" width="150" />
        <el-table-column label="商家名称" align="center" prop="merName" />
        <el-table-column label="学校" align="center" prop="school" width="200" />
        <el-table-column label="年级" align="center" prop="grade" />
        <el-table-column label="班级" align="center" prop="className" />
        <el-table-column label="身份" align="center" prop="identity" />
        <el-table-column label="支付方式" align="center" prop="paymentMethod" width="140">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.payment_method"
              :value="scope.row.paymentMethod"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column label="支付类型" align="center" prop="onlineType">
          <template slot-scope="scope">
            <span v-if="scope.row.onlineType == '1' ">在线支付</span>
            <span v-if="scope.row.onlineType == '0' ">离线支付</span>
          </template>
        </el-table-column> -->
        <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.paymentTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="补缴状态" align="center" prop="isRefunded">
          <template slot-scope="scope">
            <span v-if="scope.row.status == '1'">已补缴</span>
            <span v-if="scope.row.status == '0'">未补缴</span>
          </template>
        </el-table-column>
        <el-table-column
          label="异常原因"
          align="center"
          prop="errorMessage"
          width="180"
        />
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="padding_4_10"
              v-hasPermi="['order:abnormal:edit']"
              @click="refundStatusFun(scope.row)"
            >异常处理
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>
<script>
import {
  orderSearch,
  dealErrorOrderFlow,
  dealMoreErrorOrderFlow,
} from "@/api/order/abnormal";
export default {
  name: "abnormal",
  dicts: ["payment_method", "payment_method", "is_refunded"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消费记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        idCardNumber: null,
        personId: null,
        school: null,
        grade: null,
        className: null,
        identity: null,
        deviceNumber: null,
        orderNumber: null,
        orderSource: null,
        orderAmount: null,
        paymentMethod: null,
        paymentTime: null,
        isRefunded: null,
        refundAmount: null,
        time: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        idCardNumber: [{ required: true, message: "身份证号不能为空", trigger: "blur" }],
        personId: [{ required: true, message: "人员ID不能为空", trigger: "blur" }],
        orderNumber: [{ required: true, message: "订单号不能为空", trigger: "blur" }],
        orderAmount: [{ required: true, message: "订单金额不能为空", trigger: "blur" }],
        paymentTime: [{ required: true, message: "支付时间不能为空", trigger: "blur" }],
      },
      userId: "",
    };
  },
  watch: {
    "queryParams.time": function (newVal) {
      console.log(newVal);
      if (!newVal) {
        this.queryParams.time = [];
        this.queryParams.paymentTimeStart = null;
        this.queryParams.paymentTimeEnd = null;
      }
    },
  },
  created() {
    this.userId = window.sessionStorage.getItem("userId");
    this.getList();
  },
  methods: {
    /** 查询消费记录列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.time.length == 2) {
        this.queryParams.paymentTimeStart = this.queryParams.time[0];
        this.queryParams.paymentTimeEnd = this.queryParams.time[1];
      }
      orderSearch(this.queryParams).then((response) => {
        var list = response.rows;
        list.forEach((data) => {
          if (data.isRefunded == "1") {
            if (data.refundAmount != data.orderAmount) {
              data.isRefunded = 2;
            }
          }
        });
        this.recordList = list;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        idCardNumber: null,
        personId: null,
        school: null,
        grade: null,
        className: null,
        identity: null,
        deviceNumber: null,
        orderNumber: null,
        orderSource: null,
        orderAmount: null,
        paymentMethod: null,
        paymentTime: null,
        isRefunded: null,
        refundAmount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.queryParams.paymentTimeStart = null;
      this.queryParams.paymentTimeEnd = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    refundStatusFun(options) {
      this.$confirm("是否确认处理该异常订单？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          dealErrorOrderFlow({ id: options.id }).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess(res.msg);
            } else {
              this.$modal.msgError(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    dealMoreErrorOrderFlowFun() {
      if (this.ids.length == 0) {
        return this.$modal.msgError("请先选择异常订单");
      }
      this.$confirm("是否批量处理异常订单？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          dealMoreErrorOrderFlow({ idList: this.ids }).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess(res.msg);
            } else {
              this.$modal.msgError(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (this.form.refundAmount == 0) {
          return this.$modal.msgError("请输入正确的退款金额");
        }
        if (this.form.id != null) {
          consumptionRefund(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          consumptionRefund(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
